<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wan888888.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Model\CategoryModel;
/**
 * 活动菜单管理控制器
 * @author liuyang
 */
class CategoryController extends AdminController {

    /**
     * 菜单管理列表
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $tree = (new CategoryModel())->getTree(0,'id,name,parentid,orderno,isshow');
        $this->assign('tree', $tree); 
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取菜单树模板
        $this->meta_title = '菜单管理';
        $this->display();
    }

    /**
     * 显示菜单树，仅支持内部调
     * @param  array $tree 菜单树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /* 编辑菜单 */
    public function edit($id = null, $parentid = 0){
        $Category = (new CategoryModel());

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('编辑成功！', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = '';
            if($parentid){
                /* 获取上级菜单信息 */
                $cate = $Category->info($parentid, 'id,name,isshow,orderno');
                if(!($cate && 1 == $cate['isshow'])){
                    $this->error('指定的上级菜单不存在或被禁用！');
                }
            }

            /* 获取菜单信息 */
            $info = $id ? $Category->info($id) : '';

            $this->assign('info',       $info);
            $this->assign('category',   $cate);
            $this->meta_title = '编辑菜单';
            $this->display();
        }
    }

    /* 新增菜单 */
    public function add($parentid = 0){
        $Category = (new CategoryModel());

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = array();
            if($parentid){
                /* 获取上级菜单信息 */
                $cate = $Category->info($parentid, 'id,name,isshow,orderno');
                if(!($cate && 1 == $cate['isshow'])){
                    $this->error('指定的上级菜单不存在或被禁用！');
                }
            }

            /* 获取菜单信息 */
            $this->assign('info',       null);
            $this->assign('category', $cate);
            $this->meta_title = '新增菜单';
            $this->display('edit');
        }
    }

    /**
     * 删除一个菜单
     * @author huajie <banhuajie@163.com>
     */
    public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }

        //判断该菜单下有没有子菜单，有则不允许删除
        $child = (new CategoryModel())->where(array('parentid'=>$cate_id))->field('id')->select();
        if(!empty($child)){
            $this->error('请先删除该菜单下的子菜单');
        }

       

        //删除该菜单信息
        $res = (new CategoryModel())->delete($cate_id);
        if($res !== false){
            //记录行为
            action_log('update_category', 'category', $cate_id, UID);
            $this->success('删除菜单成功！');
        }else{
            $this->error('删除菜单失败！');
        }
    }

    /**
     * 操作菜单初始化
     * @param string $type
     * @author huajie <banhuajie@163.com>
     */
    public function operate($type = 'move'){
        //检查操作参数
        if(strcmp($type, 'move') == 0){
            $operate = '移动';
        }elseif(strcmp($type, 'merge') == 0){
            $operate = '合并';
        }else{
            $this->error('参数错误！');
        }
        $from = intval(I('get.from'));
        empty($from) && $this->error('参数错误！');

        //获取菜单
        $map = array('isshow'=>1, 'id'=>array('neq', $from));
        $list = M('Category')->where($map)->field('id,name,parentid,isshow,orderno')->select();


        //移动菜单时增加移至根菜单
        if(strcmp($type, 'move') == 0){
        	//不允许移动至其子孙菜单
        	$list = tree_to_list(list_to_tree($list));

        	$parentid =(new CategoryModel())->getFieldById($from, 'parentid');
        	$parentid && array_unshift($list, array('id'=>0,'title'=>'根菜单'));
        }

        $this->assign('type', $type);
        $this->assign('operate', $operate);
        $this->assign('from', $from);
        $this->assign('list', $list);
        $this->meta_title = $operate.'菜单';
        $this->display();
    }

    /**
     * 移动菜单
     * @author huajie <banhuajie@163.com>
     */
    public function move(){
        $to = I('post.to');
        $from = I('post.from');
        $res = (new CategoryModel())->where(array('id'=>$from))->setField('parentid', $to);
        if($res !== false){
            $this->success('菜单移动成功！', U('index'));
        }else{
            $this->error('菜单移动失败！');
        }
    }
    
    /**
     * 设置一条或者多条数据的状态
     */
    public function setShow($Model=CONTROLLER_NAME){
    
        $ids    =   I('request.ids');
        $isshow =   I('request.isshow');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
    
        $map['id'] = array('in',$ids);
        $data    =  array('isshow' => $isshow);
        $this->editRow(   "act_category" , $data, $map, '操作成功');
        
    }

    
}
